package com.demo.countdown;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CheckStartUp {

    private static List<DangerCenter> stationList;
    private static CountDownLatch countDownLatch;

    public CheckStartUp() {}

    public static boolean checkAllStations() throws Exception {
        countDownLatch = new CountDownLatch(3);

        stationList = new ArrayList<>();
        stationList.add(new StationBeijingIMooc(countDownLatch));
        stationList.add(new StationJiangsuSanling(countDownLatch));
        stationList.add(new StationShandongChangchuan(countDownLatch));

        Executor executor = Executors.newFixedThreadPool(stationList.size());

        stationList.forEach(executor::execute);

        // 等待线程执行完毕
        countDownLatch.await();
        for (DangerCenter center : stationList) {
            if (!center.isOk()) {
                return false;
            }
        }
        ((ExecutorService) executor).shutdown();
        return true;
    }

    public static void main(String[] args) throws Exception {
        boolean result = CheckStartUp.checkAllStations();
        System.out.println("监控中心针对所有危化品调度站点的检查结果为：" + result);
    }
}
